package cn.congzhou.crm.service.impl;

import cn.congzhou.crm.domain.Employee;
import cn.congzhou.crm.domain.SystemDictionaryItem;
import cn.congzhou.crm.mapper.EmployeeMapper;
import cn.congzhou.crm.mapper.PermissionMapper;
import cn.congzhou.crm.query.QueryObject;
import cn.congzhou.crm.service.IEmployeeService;
import cn.congzhou.crm.utils.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        if(id!=null){
            employeeMapper.deleteByPrimaryKey(id);
        }
        return 0;
    }


    @Override
    public Employee selectByPrimaryKey(Long id) {
        Employee emp = employeeMapper.selectByPrimaryKey(id);
        return emp;
    }

    @Override
    public List<Employee> selectAll() {
        List<Employee> emps = employeeMapper.selectAll();
        return emps;
    }

//    保存员工数据以及维护角色关系
    @Override
    public void saveOrUpdate(Employee emp, Long[] ids) {
        if(emp.getId()!=null){
            employeeMapper.updateByPrimaryKey(emp);
        }else {
//          把用户提交的密码进行MD5加密
            String password = new Md5Hash(emp.getPassword(), emp.getName()).toString();
//          重新设置到emp对象中
            emp.setPassword(password);
            employeeMapper.insert(emp);
        }
//      维护关系, 先删除关系在添加
        employeeMapper.deleteRelation(emp.getId());
        if(ids!=null && ids.length!=0){
            for (Long id : ids) {
//              添加关系
                employeeMapper.insetRelation(emp.getId(),id);
            }
        }

    }

//   分页查询
    @Override
    public PageInfo<Employee> selectByPage(QueryObject qo) {
//      在调用查询方法前执行分页方法，此方法有效一次有效
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> list = employeeMapper.selectByPage(qo);
//        创建结果集对象返回
        PageInfo<Employee> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }


    @Override
    public void checkLogin(String name, String password) {
        Employee emp = employeeMapper.selectLogin(name, password);
        if(emp==null){
            throw new RuntimeException("用户名和密码不匹配");
        }
//      把查询到的登录用户信息保存到作用域中
        UserContext.setCurrentUser(emp);

        List<String> expression = employeeMapper.selectExpressionById(emp.getId());
        UserContext.setExpressions(expression);


    }

//    批量删除数据
    @Override
    public void batchDelete(Long[] ids) {
        employeeMapper.batchDelete(ids);
    }

    @Override
    public Employee selectByName(String name) {
        Employee emp = employeeMapper.selectByName(name);
        return emp;
    }


}
